package com.bric.common;

import java.io.File;

import org.apache.log4j.Logger;

import com.bric.util.FileUtils;
import com.bric.util.StaticStr;

public class DownloadUnZip extends Thread{
	public static boolean flag=false;
	private static final Logger log=Logger.getLogger(DownloadUnZip.class);
		@Override
		public void run() {
			File temp=new File(StaticStr.localFilePath);
			if(temp.exists()){
				log.info("存放文件的临时目录之前存在，删除之...");
				FileUtils.deleteDirectory(StaticStr.localFilePath);
			}
			int trynum=0;
//			File file=FtpFile.download();
			File file=new File(StaticStr.finalZipLocal);
			if(null!=file&&file.exists()){
				log.info("从ftp中下载文件成功...");
				flag=true;
			}
			if(flag){
				log.info("开始解压文件...");
				flag=Unzip(trynum,file);
			}
		}
		private boolean Unzip(int tryNum,File file) {
			boolean flag=false;
			try {
				ZipCompressorByAnt.unzip(file.getAbsolutePath(), StaticStr.localFilePath, false);
				flag=true;
				log.info("解压成功");
			} catch (Exception e) {
				e.printStackTrace();
				log.info("解压文件出现异常");
				if(tryNum<3&&!flag){
					log.info("正在重试解压。。。");
					tryNum++;
					Unzip(tryNum,file);
				}else{
					log.info("重试三次失败，不再重试");
				}
			}
			return flag;
		}
}
